package com.yc.spider.rcw;

import com.ly.common.util.DBHelper;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

public class PersonnelHtml {
    public static void main(String[] args) throws IOException {
        URL url = new URL("https://www.0734zpw.com/jobs");
        Document doc = Jsoup.parse(url, 5000);
//        // 提取全部 a 元素
//        Elements elements = doc.select("div.td-j-name>a");
//        // 遍历 Elements 实现了 迭代器接口，可以作为集合来循环
//        for (Element element : elements) {
//            String href = element.attr("href");
////            System.out.println(href);
//            // 筛选 衡阳招聘详情 a 元素
//            if (href.matches("/j\\d+\\.html")){
//                System.out.println(href);
//                captureOne("https://www.0734zpw.com" + href);
//            }
//        }

//        循环获取分页地址
        for (int i = 45486; i < 45653; i++) {
            captureOne("https://www.0734zpw.com/j" + i +".html");
        }
    }

    public static void captureOne(String address) throws IOException {
        // String address = "https://www.0734zpw.com/j45653.html";
        URL url = new URL(address);
        // 下载解析远程地址，返回文档对象
        Document doc = Jsoup.parse(url, 5000);
        // 提取页面中的单个元素
        Element element = doc.selectFirst("div.j-n-txt");
        // 提取元素内部文本
        String name = element == null ? null : element.text();// ==> innerText
//        System.out.println(name);

        element = doc.selectFirst("div.timebg");
        // 提取元素文本
        String time = element == null ? null : element.text();

        element = doc.selectFirst("div.wage");
        // 提取元素文本
        String wage = element == null ? null : element.text();

        element = doc.selectFirst("div.itemli:nth-child(2)");
        // 提取元素文本
        String nature = element == null ? null : element.text();
        //System.out.println(p);
        //String nature = p.replaceAll("工作性质(.+)","$1");

        element = doc.selectFirst("div.itemli:nth-child(3)");
        // 提取元素文本
        String require = element == null ? null : element.text();
        //String require = q.replaceAll("年龄要求(.+)","$1");

        element = doc.selectFirst("div.itemli:nth-child(4)");
        // 提取元素文本
        String number = element == null ? null : element.text();
        //String number = r.replaceAll(".+10人.+","$1");

        element = doc.selectFirst("div.describe>div:nth-child(2)");
        // 提取元素文本
        String describe = element == null ? null : element.text();
        //System.out.println(describe);

        element = doc.selectFirst(".joblistbox");
        // 提取元素文本
        String intro = element == null ? null : element.text();
        System.out.println(intro);

        String id = address.replaceAll(".+/[j](\\d+)\\.html","$1");
        System.out.println(id);

        System.out.println(name);
        System.out.println(time);
        System.out.println(wage);
        System.out.println(nature);
        System.out.println(require);
        System.out.println(number);
        System.out.println(describe);
        System.out.println(intro);
        System.out.println(address);

        // 判断是否已经添加
        String sql = "select * from personnel where id = ?";
        long count = DBHelper.count(sql, id);
        if(count == 0){
            sql = "insert into personnel values(?,?,?,?,?,?,?,?,?,?)";
            DBHelper.update(sql, id, name, time, wage,nature,require,number,describe,intro,address);
        }
    }
}
